package com.allwees.bs.c.module.user.service.impl;

import com.allwees.bs.c.module.user.entity.FeedbackEntity;
import com.allwees.bs.c.module.user.entity.UserEntity;
import com.allwees.bs.c.module.user.repository.FeedbackRepository;
import com.allwees.bs.c.module.user.repository.UserRepository;
import com.allwees.bs.c.module.user.service.IFeedbackService;
import com.allwees.bs.c.module.user.vo.FeedbackVo;
import com.allwees.core.common.util.http.RequestContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FeedbackServiceImpl implements IFeedbackService {
    @Autowired
    private FeedbackRepository feedbackRepository;

    @Autowired
    private UserRepository userRepository;

    @Override
    public void feedback(String userUuid, FeedbackVo feedback, String channelUuid) {
        FeedbackEntity feedbackEntity = new FeedbackEntity();

        if (userUuid != null) {
            UserEntity userEntity = this.userRepository.getByUuid(userUuid);
            if (userEntity != null) {
            	feedbackEntity.setUserUuid(userEntity.getUuid());
                feedbackEntity.setUsername(userEntity.getFirstName() + " " + userEntity.getLastName());
            }
        }

        feedbackEntity.setContent(feedback.getContent());

        feedbackEntity.setType(feedback.getType());
        feedbackEntity.setEmail(feedback.getEmail());
        feedbackEntity.setPhotos(feedback.getPhotos());
        feedbackEntity.setChannelUuid(channelUuid);
        String appVersion = RequestContext.getAppVersion();
        if(StringUtils.isNotBlank(appVersion)) {
            feedbackEntity.setSoftware(appVersion);
        }
        feedbackEntity.setHardware(RequestContext.getPlatform().name());
        this.feedbackRepository.save(feedbackEntity);
    }
}
